package com.example.report.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.BaseTitle
import com.example.lib_base.UIState
import com.example.lib_http.Const
import com.example.report.R
import com.example.report.ReportIntent
import com.example.report.ReportViewModel
import com.google.gson.Gson

import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun ReportCommitPage(navHostController: NavHostController, json: String, vm: ReportViewModel = hiltViewModel()) {
    val entity = Gson().fromJson(json, ReportDialogEntity::class.java)
    //Text(text = entity.toString())

    //需要上传的日志标题
    var title by remember {
        mutableStateOf("")
    }

    //需要上传的日志内容
    var content by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        Log.e("TagA", "提交日志失败:${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        ToastUtils.showLong("提交成功")
                        navHostController.popBackStack()
                    }
                }
            }
        }
    })

    Column(
        Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.lightgray))
            .padding(10.dp)
    ) {
        BaseTitle(title = "日志编辑", navHostController = navHostController)
        Spacer(modifier = Modifier.height(10.dp))
        val time = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
        val week = TimeUtils.getChineseWeek(System.currentTimeMillis())
        Column(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White)
                .padding(start = 10.dp)
        ) {
            Text(text = "$time $week 今日", textAlign = TextAlign.Center)
        }

        Column(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Color.White)
                .padding(start = 10.dp)
        ) {
            Text(text = "今日内容")
        }
        TextField(value = title, onValueChange = { title = it }, Modifier.fillMaxWidth(), maxLines = 1, label = {
            Text(text = "请输入工作日志的标题")
        }, colors = TextFieldDefaults.colors(
            unfocusedIndicatorColor = Color.White,
            focusedIndicatorColor = Color.White,
            unfocusedContainerColor = Color.White
        )
        )
        Spacer(modifier = Modifier.height(10.dp))
        TextField(
            value = content,
            onValueChange = { content = it },
            Modifier
                .fillMaxWidth()
                .height(100.dp),
            maxLines = 5,
            label = {
                Text(text = "请输入工作日志的内容")
            },
            colors = TextFieldDefaults.colors(
                unfocusedIndicatorColor = Color.White,
                focusedIndicatorColor = Color.White,
                unfocusedContainerColor = Color.White
            )
        )
        Spacer(modifier = Modifier.height(10.dp))
        Button(onClick = {
            val map = mutableMapOf<String, String>()
            map[Const.PARAM_TITLE] = title
            map[Const.PARAM_CONTENT] = content
//            val map2 = mapOf(
//                Const.PARAM_TITLE to title,
//                Const.PARAM_CONTENT to content
//            )
            vm.sendIntent(ReportIntent.AddReport(map))
        }, modifier = Modifier.fillMaxWidth()) {
            Text(text = "提交")
        }
    }
}